﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CarInfoDBAccess
{
    public class Operation
    {

        private static Operation opt;

        public static Operation Instance
        {
            get
            {
                if (opt == null)
                    opt = new Operation();
                return opt;
            }
        }
        private Operation()
        { }

        /// <summary>
        /// 从数据库里读取所有的信息
        /// </summary>
        /// <returns></returns>
        public Model.Brand[] GetAllInfoFormDB()
        {
            DBAccess.DBAccessDataContext ddc = new DBAccess.DBAccessDataContext();
            
            var vcblist = from cb in ddc.CSBrands
                          select cb;
            List<Model.Brand> cblist = new List<Model.Brand>();
            foreach (var item in vcblist)
            {
                Model.Brand brand = new Model.Brand();
                brand.Id = item.Id;
                brand.Name = item.Name;
                brand.IsDelete = item.IsDelete;
                if (brand.CTList == null)
                    brand.CTList = new List<Model.CarType>();

                //读取车型
                var vctlist = from ct in ddc.CSCarTypes
                              where ct.BrandID == brand.Id
                              select ct;
                foreach (var cartype in vctlist)
                {

                    Model.CarType ct = new Model.CarType();
                    ct.Id = cartype.Id;
                    ct.Name = cartype.Name;
                    ct.IsDelete = cartype.IsDelete;
                    ct.BrandID = brand.Id;
                    ct.CIList = new List<Model.CarInfo>();

                    //读取车款
                    var vcilist = from ci in ddc.CSCarInfos
                                  where ci.CarTypeId == ct.Id
                                  select ci;
                    foreach (var carinfo in vcilist)
                    {
                        Model.CarInfo ci = new Model.CarInfo();
                        ci.Id = carinfo.Id;
                        ci.Name = carinfo.Name;
                        ci.Type = carinfo.Type;
                        ci.Price = carinfo.Price;
                        ci.IsDelete = carinfo.IsDelete;
                        ci.Img = carinfo.Image;
                        ci.CtID = carinfo.CarTypeId;

                        ct.CIList.Add(ci);
                    }

                    brand.CTList.Add(ct);
                }

                cblist.Add(brand);

            }
            return cblist.ToArray();
        }

        /// <summary>
        /// 存入数据库
        /// </summary>
        /// <param name="list"></param>
        public void Save2DB(Model.Brand[] list)
        {
            DBAccess.DBAccessDataContext ddc = new DBAccess.DBAccessDataContext();

            //临时做法*******
            var vcilist = from cb in ddc.CSCarInfos
                          select cb;
            ddc.CSCarInfos.DeleteAllOnSubmit(vcilist);

            var vctlist = from ct in ddc.CSCarTypes
                          select ct;
            ddc.CSCarTypes.DeleteAllOnSubmit(vctlist);

            var vcblist = from cb in ddc.CSBrands
                          select cb;
            ddc.CSBrands.DeleteAllOnSubmit(vcblist);

            ddc.SubmitChanges();

            //**************

            List<DBAccess.CSBrand> cblist = new List<DBAccess.CSBrand>();
            List<DBAccess.CSCarType> ctlist = new List<DBAccess.CSCarType>();
            List<DBAccess.CSCarInfo> cilist = new List<DBAccess.CSCarInfo>();
            
            foreach (Model.Brand brand in list)
            {
                DBAccess.CSBrand cb = new DBAccess.CSBrand();
                cb.Id = brand.Id;
                cb.Name = brand.Name;
                cb.IsDelete = 0;
                cblist.Add(cb);
                if (brand.CTList != null)
                    foreach (Model.CarType cartype in brand.CTList)
                    {
                        DBAccess.CSCarType ct = new DBAccess.CSCarType();
                        ct.Id = cartype.Id;
                        ct.Name = cartype.Name;
                        ct.IsDelete = 0;
                        ct.BrandID = cartype.BrandID;
                        ctlist.Add(ct);
                        if (cartype.CIList != null)
                            foreach (Model.CarInfo carinfo in cartype.CIList)
                            {
                                DBAccess.CSCarInfo ci = new DBAccess.CSCarInfo();
                                ci.Id = carinfo.Id;
                                ci.Name = carinfo.Name;
                                ci.Type = carinfo.Type;
                                ci.Price = carinfo.Price;
                                ci.IsDelete = 0;
                                ci.Image = carinfo.Img;
                                ci.CarTypeId = carinfo.CtID;
                                cilist.Add(ci);
                            }
                    }
            }
            ddc.CSBrands.InsertAllOnSubmit(cblist);
            ddc.CSCarTypes.InsertAllOnSubmit(ctlist);
            ddc.CSCarInfos.InsertAllOnSubmit(cilist);
            ddc.SubmitChanges();
        }

        /// <summary>
        /// 对比新抓取的数据与数据库的内容
        /// </summary>
        private void CompareWebWithDB()
        { }

        /// <summary>
        /// 更新
        /// </summary>
        private void UpdateInfo()
        { }

        /// <summary>
        /// 插入数据库
        /// </summary>
        private void Instert2DB()
        {
 
        }



    }
}
